package cn.edu.cdcas.service;

import cn.edu.cdcas.entity.Question;
import cn.edu.cdcas.entity.Student;
import cn.edu.cdcas.mapper.AnswerMapper;
import cn.edu.cdcas.mapper.QuestionMapper;
import cn.edu.cdcas.vo.QuestionNum;
import cn.edu.cdcas.vo.QuestionVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("questionService")
public class QuestionService {
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private AnswerMapper answerMapper;

    public List<QuestionVo> getQuestion(Student student, QuestionNum questionNum) {
        if (student.getExamState() == 1) {
            List<QuestionVo> vos = questionMapper.queryQuestion(student, 1);
            if (vos.size() > 0) {
                return vos;
            }
            List<Question> danxuans = questionMapper.randomDanxuan(questionNum);
            List<Question> duoxuans = questionMapper.randomDuoxuan(questionNum);
            List<Question> panduans = questionMapper.randomPanduan(questionNum);

            List<Question> questions = new ArrayList<Question>();
            questions.addAll(danxuans);
            questions.addAll(duoxuans);
            questions.addAll(panduans);

            answerMapper.insertBatch(questions, student, 1);

            return questionMapper.queryQuestion(student, 1);
        } else {
            List<QuestionVo> vos = questionMapper.queryQuestion(student, 2);
            if (vos.size() > 0) {
                return vos;
            }

            List<Question> danxuans = questionMapper.randomDanxuan(questionNum);
            List<Question> duoxuans = questionMapper.randomDuoxuan(questionNum);
            List<Question> panduans = questionMapper.randomPanduan(questionNum);

            List<Question> questions = new ArrayList<Question>();
            questions.addAll(danxuans);
            questions.addAll(duoxuans);
            questions.addAll(panduans);
            answerMapper.insertBatch(questions, student, 2);
            return questionMapper.queryQuestion(student, 2);
        }
    }
}
